package client

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"month_zh5/common/nacos"
	"month_zh5/common/proto/goods"
)

type Handler func(ctx context.Context, in goods.GoodsClient) (interface{}, error)

func GoodsClient(ctx context.Context, handler Handler) (interface{}, error) {
	nac := nacos.NacosConfig.Grpc
	dial, err := grpc.Dial(fmt.Sprintf("%s:%v", nac.Host, nac.Port), grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := goods.NewGoodsClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	defer dial.Close()
	return res, nil
}

func PageViewsTop(ctx context.Context, i *goods.PageViewsTopRequest) (*goods.PageViewsTopResponse, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, in goods.GoodsClient) (interface{}, error) {
		top, err := in.PageViewsTop(ctx, i)
		if err != nil {
			return nil, err
		}
		return top, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.PageViewsTopResponse), nil
}

func Search(ctx context.Context, i *goods.SearchRequest) (*goods.SearchResponse, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, in goods.GoodsClient) (interface{}, error) {
		top, err := in.Search(ctx, i)
		if err != nil {
			return nil, err
		}
		return top, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.SearchResponse), nil
}

func HighlightSearch(ctx context.Context, i *goods.HighlightSearchRequest) (*goods.HighlightSearchResponse, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, in goods.GoodsClient) (interface{}, error) {
		top, err := in.HighlightSearch(ctx, i)
		if err != nil {
			return nil, err
		}
		return top, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.HighlightSearchResponse), nil
}

func SearchForRecords(ctx context.Context, i *goods.SearchForRecordsRequest) (*goods.SearchForRecordsResponse, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, in goods.GoodsClient) (interface{}, error) {
		top, err := in.SearchForRecords(ctx, i)
		if err != nil {
			return nil, err
		}
		return top, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.SearchForRecordsResponse), nil
}

func ShoppingCart(ctx context.Context, i *goods.ShoppingCartRequest) (*goods.ShoppingCartResponse, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, in goods.GoodsClient) (interface{}, error) {
		top, err := in.ShoppingCart(ctx, i)
		if err != nil {
			return nil, err
		}
		return top, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.ShoppingCartResponse), nil
}

func Comment(ctx context.Context, i *goods.CommentRequest) (*goods.CommentResponse, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, in goods.GoodsClient) (interface{}, error) {
		top, err := in.Comment(ctx, i)
		if err != nil {
			return nil, err
		}
		return top, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.CommentResponse), nil
}

func CreateComment(ctx context.Context, i *goods.CreateCommentRequest) (*goods.CreateCommentResponse, error) {
	client, err := GoodsClient(ctx, func(ctx context.Context, in goods.GoodsClient) (interface{}, error) {
		top, err := in.CreateComment(ctx, i)
		if err != nil {
			return nil, err
		}
		return top, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*goods.CreateCommentResponse), nil
}
